package org.dromara.web.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.web.system.domain.AppOrder;
import org.dromara.web.system.domain.bo.AppOrderBo;
import org.dromara.web.system.domain.vo.AppOrderVo;
import org.dromara.web.system.mapper.AppOrderMapper;
import org.dromara.web.system.service.IAppOrderService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-19
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppOrderServiceImpl implements IAppOrderService {

    private final AppOrderMapper baseMapper;

    /**
     * 查询订单
     *
     * @param id 主键
     * @return 订单
     */
    @Override
    public AppOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询订单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单分页列表
     */
    @Override
    public TableDataInfo<AppOrderVo> queryPageList(AppOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppOrder> lqw = buildQueryWrapper(bo);
        Page<AppOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单列表
     *
     * @param bo 查询条件
     * @return 订单列表
     */
    @Override
    public List<AppOrderVo> queryList(AppOrderBo bo) {
        LambdaQueryWrapper<AppOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppOrder> buildQueryWrapper(AppOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppOrder> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppOrder::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), AppOrder::getOrderId, bo.getOrderId());
        lqw.eq(bo.getOrderStatus() != null, AppOrder::getOrderStatus, bo.getOrderStatus());
        lqw.eq(bo.getPlanId() != null, AppOrder::getPlanId, bo.getPlanId());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierCode()), AppOrder::getSupplierCode, bo.getSupplierCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliveryAddress()), AppOrder::getDeliveryAddress, bo.getDeliveryAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseLocation()), AppOrder::getWarehouseLocation, bo.getWarehouseLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiveAddress()), AppOrder::getReceiveAddress, bo.getReceiveAddress());
        lqw.eq(bo.getDriverSelectType() != null, AppOrder::getDriverSelectType, bo.getDriverSelectType());
        lqw.eq(bo.getIsPressCar() != null, AppOrder::getIsPressCar, bo.getIsPressCar());
        lqw.eq(bo.getPressCarFee() != null, AppOrder::getPressCarFee, bo.getPressCarFee());
        lqw.eq(StringUtils.isNotBlank(bo.getTransportCompany()), AppOrder::getTransportCompany, bo.getTransportCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getTaxpayerId()), AppOrder::getTaxpayerId, bo.getTaxpayerId());
        lqw.like(StringUtils.isNotBlank(bo.getContactName()), AppOrder::getContactName, bo.getContactName());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhone()), AppOrder::getContactPhone, bo.getContactPhone());
        lqw.like(StringUtils.isNotBlank(bo.getDriverName()), AppOrder::getDriverName, bo.getDriverName());
        lqw.eq(StringUtils.isNotBlank(bo.getDriverIdCard()), AppOrder::getDriverIdCard, bo.getDriverIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getDriverPhone()), AppOrder::getDriverPhone, bo.getDriverPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getCarPlate()), AppOrder::getCarPlate, bo.getCarPlate());
        lqw.eq(StringUtils.isNotBlank(bo.getCarType()), AppOrder::getCarType, bo.getCarType());
        lqw.eq(bo.getCarLength() != null, AppOrder::getCarLength, bo.getCarLength());
        lqw.eq(bo.getLoadCapacity() != null, AppOrder::getLoadCapacity, bo.getLoadCapacity());
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoLoadInfo()), AppOrder::getPhotoLoadInfo, bo.getPhotoLoadInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoUnloadInfo()), AppOrder::getPhotoUnloadInfo, bo.getPhotoUnloadInfo());
        lqw.eq(bo.getTotalWeight() != null, AppOrder::getTotalWeight, bo.getTotalWeight());
        lqw.eq(bo.getTareWeightLoad() != null, AppOrder::getTareWeightLoad, bo.getTareWeightLoad());
        lqw.eq(bo.getMaterialLoadWeight() != null, AppOrder::getMaterialLoadWeight, bo.getMaterialLoadWeight());
        lqw.eq(bo.getWeighingTotalWeight() != null, AppOrder::getWeighingTotalWeight, bo.getWeighingTotalWeight());
        lqw.eq(bo.getTareWeightUnload() != null, AppOrder::getTareWeightUnload, bo.getTareWeightUnload());
        lqw.eq(bo.getMaterialUnloadWeight() != null, AppOrder::getMaterialUnloadWeight, bo.getMaterialUnloadWeight());
        lqw.eq(bo.getRoadLoss() != null, AppOrder::getRoadLoss, bo.getRoadLoss());
        lqw.eq(bo.getDeductWeight() != null, AppOrder::getDeductWeight, bo.getDeductWeight());
        lqw.eq(bo.getNetWeight() != null, AppOrder::getNetWeight, bo.getNetWeight());
        lqw.eq(bo.getPackageNum() != null, AppOrder::getPackageNum, bo.getPackageNum());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseCode()), AppOrder::getWarehouseCode, bo.getWarehouseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseCarPlate()), AppOrder::getWarehouseCarPlate, bo.getWarehouseCarPlate());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseAdmin()), AppOrder::getWarehouseAdmin, bo.getWarehouseAdmin());
        lqw.eq(bo.getWarehouseDate() != null, AppOrder::getWarehouseDate, bo.getWarehouseDate());
        lqw.eq(bo.getQualityId() != null, AppOrder::getQualityId, bo.getQualityId());
        lqw.eq(bo.getFreight() != null, AppOrder::getFreight, bo.getFreight());
        lqw.eq(bo.getPressFee() != null, AppOrder::getPressFee, bo.getPressFee());
        lqw.eq(bo.getUnitPrice() != null, AppOrder::getUnitPrice, bo.getUnitPrice());
        lqw.eq(bo.getLoanAmount() != null, AppOrder::getLoanAmount, bo.getLoanAmount());
        return lqw;
    }

    /**
     * 新增订单
     *
     * @param bo 订单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppOrderBo bo) {
        AppOrder add = MapstructUtils.convert(bo, AppOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单
     *
     * @param bo 订单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppOrderBo bo) {
        AppOrder update = MapstructUtils.convert(bo, AppOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除订单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
